package com.farm;

import com.farm.chain.*;
import com.farm.command.StartWorkCommand;
import com.farm.decorator.GPSDecorator;
import com.farm.equipment.Equipment;
import com.farm.equipment.factory.EquipmentFactory;
import com.farm.equipment.factory.HarvesterMachineFactory;
import com.farm.equipment.factory.SeedingMachineFactory;
import com.farm.facade.FarmSystemFacade;
import com.farm.monitor.*;
import com.farm.path.AStarPathStrategy;
import com.farm.path.BreadthFirstStrategy;
import com.farm.path.DijkstraPathStrategy;
import com.farm.path.PathFinder;
import com.farm.proxy.EquipmentProxy;

public class Main2 {
    public static void main(String[] args) {
        // 1. 使用工厂模式创建农机
        System.out.println("=== 使用工厂模式创建农机 ===");
        EquipmentFactory factory = new SeedingMachineFactory();
        Equipment seedingMachine = factory.createEquipment("SM001", "播种机1号");
        factory=new HarvesterMachineFactory();
        Equipment harvesterMachine  = factory.createEquipment("SG001", "收割机1号");

        // 2. 使用命令模式控制农机
        System.out.println("\n=== 使用命令模式控制农机 ===");
        StartWorkCommand workCommand = new StartWorkCommand(seedingMachine);
        workCommand.execute();
        //切换为收割机执行命令
        workCommand.setEquipment(harvesterMachine);
        workCommand.execute();
        
        // 3. 使用观察者模式监控传感器
        System.out.println("\n=== 使用观察者模式监控传感器 ===");
        // 创建传感器主题
        EnvironmentSensor sensor = new EnvironmentSensor();
        // 创建观察者
        SensorObserver tempMonitor = new TemperatureMonitor();
        SensorObserver humidityMonitor = new HumidityMonitor();
        SensorObserver pressureMonitor = new PressureMonitor();
        // 注册观察者
        sensor.registerObserver(tempMonitor);
        sensor.registerObserver(humidityMonitor);
        sensor.registerObserver(pressureMonitor);
        // 模拟传感器数据变化
        System.out.println("=== 第一次数据更新 ===");
        sensor.setMeasurements(25.5, 60.0, 1013.2);
        System.out.println("\n=== 第二次数据更新 ===");
        sensor.setMeasurements(32.0, 75.0, 1005.5);
        // 移除一个观察者
        sensor.removeObserver(humidityMonitor);
        System.out.println("\n=== 第三次数据更新 (已移除湿度监控器) ===");
        sensor.setMeasurements(22.0, 90.0, 1020.0);

        
        // 4. 使用策略模式进行路径规划
        System.out.println("\n=== 使用策略模式进行路径规划 ===");
        // 初始化路径规划器，使用A*算法
        PathFinder pathFinder=new PathFinder(new AStarPathStrategy());
        System.out.println(pathFinder.findPath("起点A","终点B"));
        // 切换到Dijkstra算法（适用于权重图）
        pathFinder.setStrategy(new DijkstraPathStrategy());
        System.out.println(pathFinder.findPath("起点A","终点B"));
        // 切换到广度优先算法（适用于无权图）
        pathFinder.setStrategy(new BreadthFirstStrategy());
        System.out.println(pathFinder.findPath("起点A","终点B"));



        // 5. 使用责任链模式处理请求
        System.out.println("\n=== 使用责任链模式处理请求 ===");
        Handler logHandler = new LogHandler();
        Handler alertHandler = new AlertHandler();
        Handler emergencyHandler = new EmergencyHandler();
        logHandler.setNextHandler(alertHandler);
        alertHandler.setNextHandler(emergencyHandler);
        Request request = new Request("EMERGENCY", "设备故障");
        logHandler.handleRequest(request);



        // 6. 使用外观模式简化系统操作
        System.out.println("\n=== 使用外观模式简化系统操作 ===");
        FarmSystemFacade facade = new FarmSystemFacade();
        facade.startFarmOperation();
        facade.stopFarmOperation();

        // 7. 使用代理模式控制设备访问
        System.out.println("\n=== 使用代理模式控制设备访问 ===");
        EquipmentProxy proxy = new EquipmentProxy(seedingMachine, "USER");
        proxy.work(); // 应该被拒绝
        proxy = new EquipmentProxy(seedingMachine, "ADMIN");
        proxy.work(); // 应该被允许

        // 8. 使用装饰器模式添加GPS功能
        System.out.println("\n=== 使用装饰器模式添加GPS功能 ===");
        Equipment gpsEquipment = new GPSDecorator(seedingMachine);
        gpsEquipment.work();
        ((GPSDecorator) gpsEquipment).updateLocation();
    }
} 